﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking.Types;
using UnityEngine.ProBuilder.MeshOperations;
using UnityEngine.UI;

public class UserHomePanel : AppUI
{
    public PhotosWidget photosWidget;
    public EditPhotosWidget editPhotosWidget;
    public UserInfoWidget userInfoWidget;
    public UserVerifyWidget userVerifyWidget;
    public MomentWidget momentWidget;
    public GameObject editMomentBtn;
    public GameObject momentContentObj;
    public AboutMeWidget aboutMeWidget;
    public GameObject shareBtn;
    public GameObject reportBtn;
    public EditIconWidget editIconWidget;

    public GameObject applyBtn;
    public GameObject viewApplyBtn;
    public GameObject chatBtn;

    public Image likeBtnImg;
    public Color likeBtnActiveColor = Color.white;
    public Color likeBtnDisactiveColot = Color.white;

    UserMomentsInfo m_UserMomentsInfo;

    public override void Awake()
    {
        base.Awake();

        editIconWidget.onEditEnd = OnLoadedImg;
        HideEditIconWidget();

        applyUI.gameObject.SetActive(false);
    }

    public void Init(UserMomentsInfo UserMomentsInfo)
    {
        m_UserMomentsInfo = UserMomentsInfo;

        bool isMe = UserMomentsInfo.user.userId == MGame.instance.MUser.id;

        userInfoWidget.gameObject.SetActive(true);
        userVerifyWidget.gameObject.SetActive(false);//TODO.
        aboutMeWidget.gameObject.SetActive(true);

        var userInfo = UserMomentsInfo.user;

        PhotosData photosData = new PhotosData(userInfo);

        RefreshPhotosWidget(photosData);

        editPhotosWidget.gameObject.SetActive(isMe);
        editPhotosWidget.Init(photosData, RefreshPhotosWidget);

        userInfoWidget.Init(userInfo);
        userVerifyWidget.Init(userInfo);

        if (UserMomentsInfo.moments.Length > 0)
        {
            momentWidget.gameObject.SetActive(true);
            momentWidget.Init(UserMomentsInfo.moments[UserMomentsInfo.moments.Length - 1], UserMomentsInfo.moments.Length);
            editMomentBtn.gameObject.SetActive(false);
            momentContentObj.gameObject.SetActive(true);
        }
        else
        {
            if (UserMomentsInfo.user.userId == MGame.instance.MUser.id)
            {
                momentWidget.gameObject.SetActive(true);
                momentWidget.Init(null, 0);
                editMomentBtn.gameObject.SetActive(true);
                momentContentObj.gameObject.SetActive(false);
            }
            else
            {
                momentWidget.gameObject.SetActive(false);
                editMomentBtn.gameObject.SetActive(false);
            }
        }

        aboutMeWidget.Init(userInfo);

        reportBtn.SetActive(!isMe);
        shareBtn.SetActive(!isMe);

        if (this.m_UserMomentsInfo.user.userId == MGame.instance.MUser.id)
        {
            this.chatBtn.SetActive(false);
            this.applyBtn.SetActive(false);
            this.viewApplyBtn.SetActive(false);
            this.likeBtnImg.gameObject.SetActive(false);
        }
        else
        {
            var apply = MGame.instance.MUser.applies.Find(e => e.targetUserId == this.m_UserMomentsInfo.user.userId);
            bool friend = MGame.instance.MUser.friends.Contains(this.m_UserMomentsInfo.user.userId);
            bool existApply = apply != null;
            this.chatBtn.SetActive(friend);
            this.applyBtn.SetActive(!existApply && !friend);
            this.viewApplyBtn.SetActive(existApply && !friend);
        }

        SetLikeBtn(!MGame.instance.MUser.likes.Contains(UserMomentsInfo.user.userId));
    }
    public void Refresh(Moment moment)
    {
        if (m_UserMomentsInfo != null)
        {
            UserMoment userMoment = new UserMoment(m_UserMomentsInfo.user, moment);
            m_UserMomentsInfo.moments = m_UserMomentsInfo.moments.ArrayAdd(userMoment);
            this.Init(m_UserMomentsInfo);

            GameDebug.Log($"user home 刷新:{m_UserMomentsInfo.moments.Length}");
        }
        //m_UserMomentsInfo.m;
    }


    public void RefreshPhotosWidget(PhotosData userInfo)
    {
        bool isMe = m_UserMomentsInfo.user.userId == MGame.instance.MUser.id;

        int photosCount = photosWidget.InitTextures(isMe, userInfo);
        photosWidget.gameObject.SetActive(photosCount > 0);
    }


    // Start is called before the first frame update
    public override void Start()
    {
        base.Start();
    }

    void SetLikeBtn(bool active)
    {
        this.likeBtnImg.color = active ? this.likeBtnActiveColor : this.likeBtnDisactiveColot;
        likeBtnImg.GetComponent<Button>().interactable = active;
    }

    public void RefreshIntroduction(int type, string content)
    {
        if (type == IntroductioType.aboutMe)
            m_UserMomentsInfo.user.aboutMe = content;
        if (type == IntroductioType.familyBackground)
            m_UserMomentsInfo.user.familyBackground = content;
        if (type == IntroductioType.interest)
            m_UserMomentsInfo.user.interest = content;
        if (type == IntroductioType.loveView)
            m_UserMomentsInfo.user.loveView = content;
        if (type == IntroductioType.idealCompanion)
            m_UserMomentsInfo.user.idealCompanion = content;

        aboutMeWidget.Init(m_UserMomentsInfo.user);
    }
    public void OnClickShareUser()
    {
        GameDebug.Log($"UserHomePanel -> OnClickShareUser");
    }
    public void OnClickReport()
    {
        GameDebug.Log($"UserHomePanel -> OnClickReport");
    }
    public void OnLoadedImg(Texture2D texture)
    {
        if (texture != null)
        {
            photosWidget.OnLoadedImg(texture);
        }

        HideEditIconWidget();
    }
    public void HideEditIconWidget()
    {
        editIconWidget.gameObject.SetActive(false);
    }
    public void ShowEditIconWidget()
    {
        editIconWidget.gameObject.SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void ShowUserMomentsPanel()
    {
        var ui = UIController.ShowUI<UserMomentsPanel>();
        ui.Init(this.m_UserMomentsInfo);
    }

    #region Apply
    public ApplyUI applyUI;
    public void OnClickShowAppUI()
    {
        applyUI.gameObject.SetActive(true);
        applyUI.Init(this.m_UserMomentsInfo.user);
    }
    public void OnClickCloseAppUI()
    {
        applyUI.gameObject.SetActive(false);
    }
    public void OnClickViewApply()
    {
        var apply = MGame.instance.MUser.applies.Find(e => e.targetUserId == this.m_UserMomentsInfo.user.userId);
        if (apply != null)
        {
            ApplyUserPanel view = UIController.ShowUI<ApplyUserPanel>();
            view.Init(apply);
        }
        else
        {
            GameDebug.LogError($"cannot find apply for:{this.m_UserMomentsInfo.user.userId}");
        }
    }
    public void OnClickChat()
    {
        var view = UIController.ShowUI<ChatPanel>();
        ChatData chatData = new ChatData()
        {
            myUserId = MGame.instance.MUser.id,
            opponentUserId = m_UserMomentsInfo.user.userId,
            myChat = MGame.instance.MUser.chats.Find(e=>e.userId1 == MGame.instance.MUser.id),
            opponentChat = MGame.instance.MUser.chats.Find(e => e.userId1 == m_UserMomentsInfo.user.userId),
        };
        view.Init(m_UserMomentsInfo.user, chatData);
    }
    public void OnClickLike()
    {
        RequestServerController.instance.ReqLike(MGame.instance.MUser.id,this.m_UserMomentsInfo.user.userId,  (res) =>
        {
            SetLikeBtn(false);

            GameDebug.Log($"like:{this.m_UserMomentsInfo.user.userId} end");
        });
    }

    #endregion
}
